package com.baoai.controller;


import com.baoai.dto.APIResponse;
import com.baoai.dto.SearchIndexDto;
import com.baoai.service.SearchService;
import com.baoai.util.LuenceSearchUtil;
import org.codehaus.jackson.map.ObjectMapper;
import org.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by xiaren on 2017/3/3.
 */
@WebServlet("/serlvet/SearchIndexController")
public class SearchIndexController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    private SearchService searchService = new SearchService();

    /**
     * @see HttpServlet#HttpServlet()
     */
    public SearchIndexController() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF8");
        response.setCharacterEncoding("UTF8");
        response.setContentType("application/json; charset=UTF8");

        PrintWriter out = response.getWriter();
        String method = (String) request.getParameter("method");
        APIResponse<Object> apiResponse = operate(method, request, response);
        try {
            ObjectMapper mapper = new ObjectMapper();
            out.print(LuenceSearchUtil.bean2Json(apiResponse));
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    /**
     * 接口转换
     * @param method search:查询,addOrUpdate:添加或更新,delete:删除
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    private APIResponse<Object> operate(String method, HttpServletRequest request, HttpServletResponse response) throws IOException {
        APIResponse<Object> apiResponse = new APIResponse<>();
        SearchService searchService = new SearchService();

        String id = request.getParameter("id");
        String searchText = request.getParameter("searchText");
        String pageIndex = request.getParameter("pageIndex");
        String pageSize = request.getParameter("pageSize");
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        String srcJson = request.getParameter("srcJson");
        String power = request.getParameter("power");
        String labelJson = request.getParameter("labelJson");
        String number = request.getParameter("number");
        String time = request.getParameter("time");
        String topicId = request.getParameter("topicId");
        String presentPrice = request.getParameter("presentPrice");
        String oldPrice = request.getParameter("oldPrice");
        String author = request.getParameter("author");
        String titleName = request.getParameter("titleName");
        String address = request.getParameter("address");


        // 类型【1：视频、2问答、3课程、4文章、5专家】
        String type = request.getParameter("type");
        if ("search".equals(method)) {  // 查询
            if (pageIndex != null && pageSize != null && LuenceSearchUtil.isNumeric(pageIndex) && LuenceSearchUtil.isNumeric(pageSize) && type != null) {
                apiResponse = searchService.searchIndex(searchText, type, Integer.valueOf(pageIndex), Integer.valueOf(pageSize));
            } else {
                apiResponse.setStatus(apiResponse.FAIL);
                apiResponse.setMessage("参数错误!");
            }
        } else if("searchAllIndex".equals(method)){     // 查询所有
            if (type != null) {
                apiResponse = searchService.searchAllIndex(type);
            } else {
                apiResponse.setStatus(apiResponse.FAIL);
                apiResponse.setMessage("参数错误!");
            }
        }  else if ("addOrUpdate".equals(method)) { // 添加或更新
            if (id != null && type != null) {
                SearchIndexDto searchIndexDto = new SearchIndexDto();

                searchIndexDto.setId(id);
                searchIndexDto.setType(type);
                searchIndexDto.setTitle(title);
                searchIndexDto.setContent(content);
                searchIndexDto.setSrcArrays(LuenceSearchUtil.json2Arrays(srcJson));
                searchIndexDto.setPower(LuenceSearchUtil.int2String(power));
                searchIndexDto.setLabelArrays(LuenceSearchUtil.json2Arrays(labelJson));
                searchIndexDto.setNumber(LuenceSearchUtil.int2String(number));
                searchIndexDto.setTime(LuenceSearchUtil.int2String(time));
                searchIndexDto.setTopicId(LuenceSearchUtil.int2String(topicId));
                searchIndexDto.setPresentPrice(LuenceSearchUtil.int2String(presentPrice));
                searchIndexDto.setOldPrice(LuenceSearchUtil.int2String(oldPrice));
                searchIndexDto.setAuthor(author);
                searchIndexDto.setTitleName(titleName);
                searchIndexDto.setAddress(address);

                searchService.saveOrUpdateIndex(searchIndexDto);
            } else {
                apiResponse.setStatus(apiResponse.FAIL);
                apiResponse.setMessage("参数错误!");
            }
        } else if ("delete".equals(method)) {  // 删除

            if (id != null && type != null) {
                apiResponse = searchService.deleteIndex(id, type);
            } else {
                apiResponse.setStatus(apiResponse.FAIL);
                apiResponse.setMessage("参数错误!");
            }
        }
        return apiResponse;
    }




}
